from etgui import *
from PageLayout import *
# A Dialog edits the values of a PotGuiModel.
# You can set the values with
#   model.Set("SlotName",value)
# and get the values with
#    model.Get("SlotName")
# The GuiElements are bound to the values.
# When the value is changed in the gui, the model
# value is automatically changed.
# And if you change the model value the GUI value
# is changed too.

def MakeView():
	"""Create the GUI elements"""
	# The Popup changes the value "ChoiceValue" in
	# the model
	popup=PotGuiPopupButton("ChoiceValue")
	# AddItem(display_name,value_in_model)
	# the value_in_model could also be an integer
	popup.AddItem("Do This","this")
	popup.AddItem("Do That","that")
	popup.AddItem("Item 3","value3")

	view=MakePageLayout(
		eVObjHExpand|eVObjVExpand, # how to align the items on the page
		Form(
			# the items are pairs of label,gui-element
			items=[
				"Text Label 1", PotTextField("Text1"), # edit "Text1" in model
				"Password", PasswordField("Password"), # edit "Text1" in model
				"Popup label",  popup,
			]
		),
		# the "MultiLineText" in the model is a TextView
		PotGuiTextView("MultiLineText",TextView(),Point(200))
	)
	return view

def p(model,slot):
	"""Print the value of a model slot"""
	print `slot`,"=",`model.GetValue(slot)`

# important: keep one copy of the dialog
gDialog=None
def Show():
	global gDialog
	if not gDialog:
		# create the dialog once!
		model=PotGuiModel()
		gDialog=MakeOkCancelDialog("My Dialog",model,MakeView())
		# set this value only the first time and
		# remember when dialog is opened next time
		model.SetValue("Text1","bla")
	# get the dialogs model
	model=gDialog.GetModel()
	# set the following values each time the dialog is shown
	model.SetValue("ChoiceValue","that")
	model.SetValue("MultiLineText","my\ntext")

	# show the dialog
	if gDialog.ShowUnderMouse()==CmdNo.cIdOk:
		# The user pressed OK. Do soemthing!
		# Let's print the values of the model slots
		p(model,"Text1")
		p(model,"ChoiceValue")
		p(model,"MultiLineText")
		p(model,"Password")
